<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./vue.js"></script>
</head>
<body>
  <div id="app">
    <home></home>
  </div>
  <script>
    // 事件总线 （中央事件总线）

    /* 
    利用了 第三方实例
      this.$emit("事件名",参数);
      this.$on("事件名",fn)
    问题：谁弹射，就谁监听
    */
    let eventBus = new Vue();
    let CommonTitle = {
   
      template:`
        <div>
            子组件1
            <button @click="biubiubiu">点击传数据</button>
        </div>
      `,
      data(){
        return {
          msg:"我是子组件1的数据"
        }
      },
      methods:{
        biubiubiu(){
          eventBus.$emit("piu",this.msg)
          // 组件 实例 上  方法  $emit()  eject
        }
      }
    }
    let CommonTitle2 = {
   
      template:`
        <div>
            子组件2
            {{msg}}
        </div>
      `,
      mounted(){
        /* eventBus.$on("piu",(msg)=>{
          // alert(msg)
          this.msg = msg;
        }) */
        let _this = this;
        /* eventBus.$on("piu",function(msg){
          _this.msg = msg;
        }) */
        eventBus.$on("piu",function(msg){
          this.msg = msg;
        }.bind(this))
      },
      data(){
        return {
          msg:"我是子组件2的数据"
        }
      }
    }
    let Home = {
      template:`
        <div>
          <common-title></common-title>  
          <hr/>
          <common-title2></common-title2>  
          home页内容  
        </div>
      `,
      data(){
        return {
          msg:"我是父组件的数据"
        }
      },
     
      components:{
        CommonTitle,
        CommonTitle2
      }
    }
    let vm = new Vue({
      el:"#app",
      components:{
        Home
      }
    })
  </script>

</body>
</html>